home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / 3.2v4 / fman next >
Encoding:
Korn shell script  |  1997-08-26  |  6.8 KB  |  259 lines

  1. #!/bin/ksh
  2. # fman: new man program
  3. # @(#) fman.ksh 1.5 94/04/16
  4. # 91/07/03 john h. dubois iii (john@armory.com)
  5. # 91/07/11 made it unpack man pages if neccessary
  6. # 91/07/16 fixed test for whether man file pattern was expanded
  7. # 92/01/21 made it read /etc/default/man to get section order,
  8. #          and only display the first section found.
  9. # 92/02/06 changed name to fman
  10. # 92/02/07 fixed bug in notfound
  11. # 92/02/13 incorporated changes from DOS version
  12. # 92/03/11 changed to use MANPATH from environment if set,
  13. #          and search all directories given in MANPATH
  14. # 92/03/15 exec pager or man w/o forking
  15. # 92/05/31 try using index if one exists
  16. # 92/10/01 Added "See also <other sections>"
  17. # 92/10/18 If PAGER is less, search for name of man page to make it easier
  18. #          to find information in man pages for multiple items
  19. # 92/11/11 Make it work for compressed files not listed in index;
  20. #          deal with man pages listed in index that don't exist.
  21. # 93/03/30 Fixed bug in MANPATH processing
  22. # 93/06/17 Include paths in "See also:" message if they would be needed
  23. #          to get to a man page.  Allow MANPATH spec on command line.
  24. # 93/07/09 Added -h and -e options.
  25. # 94/04/16 Added x option.
  26.  
  27. alias istrue="test 0 -ne"
  28. alias isfalse="test 0 -eq"
  29.  
  30. # Finds all sections that man page $1 is in and puts them in the the
  31. # global array Sections[].  
  32. # The filename of each page is put in FileNames[] with the same index.
  33. # Global vars used:
  34. # patharr[] MANPATH directories.
  35.  
  36. function FindSectionsInIndex {
  37.     typeset index indexes section mpath page=$1
  38.     typeset -i i=0 NIndex=0
  39.  
  40.     for mpath in "${patharr[@]}"; do
  41.     if [ -r $mpath/index ]; then
  42.         indexes="$indexes $mpath/index"
  43.         let NIndex+=1
  44.     fi
  45.     done
  46.     [ -z "$indexes" ] && return
  47.     # Make egrep give filename
  48.     [ NIndex -lt 2 ] && indexes="$indexes /dev/null"
  49.     # set positional parameters to
  50.     # indexfile:searchname pagename section ...
  51.     # e.g.
  52.     # /usr/man/index:FP_OFF Routines DOS
  53.     set -- `egrep "^$page[     ]" $indexes`
  54.     while [ $# -gt 2 ]; do
  55.     FileNames[i]=${1%%index*}cat.$3/$2.$3
  56.     Sections[i]=$3
  57.     shift 3
  58.     let i+=1
  59.     done
  60. }
  61.  
  62. # Finds all sections that man page $1 is in by searching each man directory
  63. # in the order given in patharr[],
  64. # and puts them in the the global array Sections[].  
  65. # The filename of each page is put in FileNames[] with the same index.
  66. # Global vars used:
  67. # patharr[] MANPATH directories.
  68. function FindSectionsInDirs {
  69.     typeset page=$1 mpath AllPaths Path
  70.     typeset -i i
  71.  
  72.     for mpath in "${patharr[@]}"; do
  73.     AllPaths="$AllPaths $mpath/cat.*/$page.*"
  74.     done
  75.  
  76.     i=0
  77.     for Path in $AllPaths; do
  78.     if [[ "$Path" != *\* ]]; then
  79.         # Remove compressed-file suffix to make FileNames be the same
  80.         # as it is when built by FindSectionsInIndex()
  81.         FileNames[i]=${Path%.[zZ]}
  82.         Path=${Path%/*}
  83.         Sections[i]=${Path##*/*.}
  84.         let i+=1
  85.     fi
  86.     done
  87. }
  88.  
  89. # FindSection: display man page.
  90. # Uses ordarr[] (built from $ORDER) to display the version of the man
  91. # page that occurs first in $ORDER.
  92. # Sections[] gives the sections that a man page was found in.
  93. # If the global variable "exist" is set to 1, nothing is displayed;
  94. # the function instead returns zero if a page is found, nonzero if not.
  95. # The filename of each page is in FileNames[] with the same index.
  96. # Global vars used:
  97. # Sections[], FileNames[], ordarr[]
  98. function FindSection {
  99.  
  100.     typeset -i NumPages i foundsec
  101.     typeset section OtherSec filename NPAGER=$PAGER POpt page=$1 Pat
  102.     typeset PageFile
  103.  
  104.     NumPages=${#Sections[*]}    # Number of versions of man page found.
  105.     isfalse NumPages && return 1 
  106.     [[ "$PAGER" = *less ]] && POpt="-p$page"
  107.  
  108.     # For each section in ORDER, determine if any man page was found in
  109.     # that section
  110.     for section in "${ordarr[@]}"; do
  111.     i=0
  112.     foundsec=0
  113.     while [ i -lt NumPages ]; do
  114.         if [ "${Sections[i]}" = $section ]; then
  115.         # Found a man page from this section of ORDER
  116.         filename=${FileNames[i]}
  117.         if [ -z "$PageFile" ]; then
  118.             PageFile=$filename
  119.         else
  120.             if istrue foundsec; then
  121.             OtherSec="$OtherSec$page(${filename%/*/*} $section) "
  122.             else
  123.             OtherSec="$OtherSec$page($section) "
  124.             fi
  125.         fi
  126.         foundsec=1
  127.         fi
  128.         let i+=1
  129.     done
  130.     done
  131.     # No pages with the specified section found.
  132.     [ -z "$PageFile" ] && return 1
  133.     # Return if all we want to know is whether the man page exists.
  134.     [ "$exist" = 1 ] && return 0
  135.     if [ -z "$OtherSec" ]; then
  136.     NPAGER="exec $PAGER"
  137.     fi
  138.     if [ -r $PageFile ]; then
  139.     $NPAGER $POpt $PageFile
  140.     elif [ -r $PageFile.z ]; then
  141.     pcat $PageFile.z | $NPAGER $POpt
  142.     elif [ -r $PageFile.Z ]; then
  143.     zcat $PageFile.Z | $NPAGER $POpt
  144.     else
  145.     print -u2 "$PageFile: cannot open."
  146.     OtherSec=
  147.     unset Sections[i]
  148.     let i+=1
  149.     continue
  150.     fi
  151.     echo "See also $OtherSec"
  152.     exit 0
  153. }
  154.  
  155. # main program
  156.  
  157. typeset -i exist=0 debug=0
  158.  
  159. name=${0##*/}
  160. Usage="Usage: $name [-eh] [[manpath] section] command-name"
  161.  
  162. while getopts :hex opt; do
  163.     case $opt in
  164.     h)
  165.     echo \
  166. "$name: print man pages.
  167. $name locates and prints the specified manual pages from the online UNIX
  168. documentation.
  169. $Usage
  170. Options:
  171. -e: Determine whether the specified man page exists.  Nothing is printed;
  172.     $0 exits with a zero status if the page exists and a nonzero status if
  173.     it does not.
  174. -h: Print this help."
  175.        exit 0
  176.        ;;
  177.      e)
  178.     exist=1
  179.     ;;
  180.      x)
  181.     debug=1
  182.     ;;
  183. #    f)
  184. #    files="$files $OPTARG"    # add arg to list of address files
  185. #    ;;
  186.     +?)
  187.     print -u2 "$name: options should not be preceded by a '+'."
  188.     exit 1
  189.     ;;
  190.     ?) 
  191.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  192.     exit 1
  193.     ;;
  194.     esac
  195. done
  196.  
  197. # remove args that were options
  198. let OPTIND=OPTIND-1
  199. shift $OPTIND
  200.  
  201. if [ $# -lt 1 ]; then
  202.     print -u2 "$Usage\nUse -h for help."
  203.     exit
  204. fi
  205.  
  206. P=$PAGER
  207. O=$ORDER
  208. T=$TERM
  209. M=$MANPATH
  210. . /etc/default/man
  211. [ -n "$P" ] && PAGER=$P
  212. [ -n "$O" ] && ORDER=$O
  213. [ -n "$T" ] && TERM=$T
  214. [ -n "$M" ] && MANPATH=$M
  215.  
  216. case $# in
  217. 0)  print "No man page specified."
  218.     exit 1;;
  219. 1)  page=$1;;
  220. 2)  typeset -u ORDER=$1
  221.     page=$2;;
  222. 3)  MANPATH=$1
  223.     [ -n "$2" ] && typeset -u ORDER=$2
  224.     page=$3;;
  225. *)  print "Too many arguments."
  226.     exit 1;;
  227. esac
  228.  
  229. args=$*
  230. [ ! -t 0 ] && PAGER=cat
  231. OIFS=$IFS
  232. IFS=:
  233. set -A ordarr $ORDER
  234. set -A patharr $MANPATH
  235. IFS=$OIFS
  236. # if less or more is being used, remove multiple blank lines
  237. export LESS="-s $LESS"
  238. export MORE="-s $MORE"
  239.  
  240. # Try using index
  241. FindSectionsInIndex "$page"
  242. # Exit 0 if a page was found and we're just testing for existence.
  243. FindSection "$page" && exit 0
  244.  
  245. # Try searching directories
  246. unset Sections[*]
  247. FindSectionsInDirs "$page"
  248. FindSection "$page" && exit 0
  249.  
  250. istrue exist && exit 1
  251.  
  252. # Try using man
  253. # If using more or less, make man run faster by letting more or less compress
  254. # multiple blank lines instead of rmb
  255. [[ "$PAGER" = *@(more|less) ]] && manopt=-b
  256. set -A cmd -- /usr/bin/man $manopt -p$PAGER $args
  257. istrue debug && print -u2 "$name: running ${cmd[*]}"
  258. exec "${cmd[@]}"
  259.